//
//  ResolveJSON.m
//  MortgageCalutar
//
//  Created by johnas on 13-11-12.
//  Copyright (c) 2013年 rong360. All rights reserved.
//

#import "ParseUtil.h"
#import "Config.h"
#import "CityInfo.h"
#import "ProductEntity.h"
#import "RecommendEntity.h"
static ParseUtil* rsj = nil;

@implementation ParseUtil

+ (ParseUtil*)shareParser{
    
    @synchronized(self){
        
        if (rsj == nil) {
            
           rsj = [[ParseUtil alloc] init];
        }
    }
    
    return rsj;
}

//获得提交问题反馈
- (void)getQuestionFeedback:(NSData *)jsonData{
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
    NSString *str = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"Json Str: %@",str);
    NSString *askID = [dic objectForKey:@"ask_id"];
    NSLog(@"askID Str: %@",askID);
    [[NSNotificationCenter defaultCenter]postNotificationName:kAskQuestionSuccess object:self userInfo:@{@"QuesID":askID}];
}

//获得问题答案
- (void)getReply:(NSData *)jsonData{
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
    NSString *str = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"Json Str: %@",str);
    NSArray *answers = [dic objectForKey:@"reply"];
    NSLog(@"Answer Str: %@",answers);
    for (NSDictionary *dic in answers) {
        NSString *currentTime = [[ToolOperation shareTool]stringOfCurrentDateTool2];
        NSString *quesID = [[dic allKeys]lastObject];
        MsgInfo *msgInfo = [[[DBMan shareDB]getAllReplysByQuestionID:quesID]lastObject];
        if ([[DBMan shareDB]isReplyByQuesID:quesID]) {
            if ([[dic objectForKey:quesID] isEqualToString:msgInfo.reply] == NO) {//当回答内容与上次不一致时插入数据
                MsgInfo *newMsgInfo = [[MsgInfo alloc]initWithQuesID:quesID quesStr:msgInfo.quesStr reply:[dic objectForKey:quesID] time:currentTime bRead:0 bReply:1];
                [[DBMan shareDB]saveToQA:newMsgInfo];
            }
    
        }else{
            [[DBMan shareDB]updateAnswer:[dic objectForKey:quesID] byID:quesID];
        }
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:kNewMsgNotification object:self userInfo:nil];
}

- (void)getProductInfo:(NSData *)jsonData{
    NSError *error;
    NSDictionary *productDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
    NSDictionary *dataDic = [productDic objectForKey:@"data"];
    NSLog(@"====>Status：%@\n",[productDic objectForKey:@"status"]);
    if (error != nil) {
        NSLog(@"Error:%@\n",[error description]);
    }else{
        [[NSNotificationCenter defaultCenter]postNotificationName:kGetProductSuccess object:self userInfo:nil];
        [[ProductEntity sharedProduct]parseFromDic:dataDic];
    }
}

- (void)getProductInfoFromCache{
    NSData *productData = [[NSUserDefaults standardUserDefaults]objectForKey:kProductInfo];
    if (productData) {
        [self getProductInfo:productData];
    }
}

- (void)getRecommendInfo:(NSData *)jsonData{
    NSError *error;
    NSDictionary *recommendDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
    NSDictionary *dataDic = [recommendDic objectForKey:@"data"];
    NSLog(@"====>Status：%@\n",[recommendDic objectForKey:@"status"]);
    if (error != nil) {
        NSLog(@"Error:%@\n",[error description]);
    }else{
        [[RecommendEntity sharedRecommend]parseFromDic:dataDic];
    }
}

//获取配置信息
- (void)getConfig:(NSData *)jsonData{
    
    Config *config = [Config shareConfig];
                      
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
    NSString *str = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"Json Str: %@",str);
    
    if (error != nil) {
        
        NSLog(@"Error %@", [error localizedDescription]);
        [config getConfigFromCache];//如果请求出现异常，从缓存获取
        
    }else{
        NSDictionary *finalDic = [dic objectForKey:@"rate"];
        
        config.yingyeshui = [finalDic objectForKey:@"yinyeshui"];
        NSLog(@"yinyeshui:%@\n",config.yingyeshui);
        config.geren_quan = [finalDic objectForKey:@"geren_quan"];
        NSLog(@"geren_quan:%@\n",config.geren_quan);
        config.geren_cha = [finalDic objectForKey:@"geren_cha"];
        NSLog(@"geren_cha:%@\n",config.geren_cha);
        config.chenjian = [finalDic objectForKey:@"chenjian"];
        NSLog(@"chenjian:%@\n",config.chenjian);
        config.jiaoyufujia = [finalDic objectForKey:@"jiaoyufujia"];
        NSLog(@"jiaoyufujia:%@\n",config.jiaoyufujia);
        config.dijiakuan_quan = [finalDic objectForKey:@"dijiakuan_quan"];
        NSLog(@"dijiakuan_quan:%@\n",config.dijiakuan_quan);
        config.dijiakuan_cha = [finalDic objectForKey:@"dijiakuan_cha"];
        NSLog(@"dijiakuan_cha:%@\n",config.dijiakuan_cha);
        config.qishui_1dang = [finalDic objectForKey:@"qishui_1dang"];
        NSLog(@"qishui_1dang:%@\n",config.qishui_1dang);
        config.qishui_2dang = [finalDic objectForKey:@"qishui_2dang"];
        NSLog(@"qishui_2dang:%@\n",config.qishui_2dang);
        config.qishui_3dang = [finalDic objectForKey:@"qishui_3dang"];
        NSLog(@"qishui_3dang:%@\n",config.qishui_3dang);
        config.zhongjie = [finalDic objectForKey:@"zhongjie"];
        NSLog(@"zhongjie:%@\n",config.zhongjie);
        config.jizhun_house = [finalDic objectForKey:@"house_jizhun"];
        NSLog(@"jizhun_house:%@\n",config.jizhun_house);
        config.jizhun_gjj = [finalDic objectForKey:@"gongjijin_jizhun"];
        NSLog(@"jizhun_gjj:%@\n",config.jizhun_gjj);
        config.yinghua = [finalDic objectForKey:@"yinghua"];
        NSLog(@"yinghua:%@\n",config.yinghua);
        
        //保存到本地缓存
        [[NSUserDefaults standardUserDefaults]setObject:config.yingyeshui forKey:@"yinyeshui"];
        [[NSUserDefaults standardUserDefaults]setObject:config.geren_quan forKey:@"geren_quan"];
        [[NSUserDefaults standardUserDefaults]setObject:config.geren_cha forKey:@"geren_cha"];
        [[NSUserDefaults standardUserDefaults]setObject:config.chenjian forKey:@"chenjian"];
        [[NSUserDefaults standardUserDefaults]setObject:config.jiaoyufujia forKey:@"jiaoyufujia"];
        [[NSUserDefaults standardUserDefaults]setObject:config.dijiakuan_quan forKey:@"dijiakuan_quan"];
        [[NSUserDefaults standardUserDefaults]setObject:config.dijiakuan_cha forKey:@"dijiakuan_cha"];
        [[NSUserDefaults standardUserDefaults]setObject:config.qishui_1dang forKey:@"qishui_1dang"];
        [[NSUserDefaults standardUserDefaults]setObject:config.qishui_2dang forKey:@"qishui_2dang"];
        [[NSUserDefaults standardUserDefaults]setObject:config.qishui_3dang forKey:@"qishui_3dang"];
        [[NSUserDefaults standardUserDefaults]setObject:config.zhongjie forKey:@"zhongjie"];
        [[NSUserDefaults standardUserDefaults]setObject:config.jizhun_house forKey:@"house_jizhun"];
        [[NSUserDefaults standardUserDefaults]setObject:config.jizhun_gjj forKey:@"gongjijin_jizhun"];
        [[NSUserDefaults standardUserDefaults]setObject:config.yinghua forKey:@"yinghua"];
    }
}

//获取城市列表
-(void)getCities:(NSData *)cityJson;{
    
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:cityJson options:NSJSONReadingMutableLeaves error:&error];
    NSDictionary *cityDic = [dic objectForKey:@"city"];
//    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
//                                                              NSUserDomainMask, YES) objectAtIndex:0];
//    
//    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"citylist.plist"];
//    if(cityDic) {
//        
//        [cityDic writeToFile:plistPath atomically:YES];
//        
//    }
    [[NSUserDefaults standardUserDefaults]setObject:cityDic forKey:kCity];//保存数据

}


@end
